#![allow(dead_code)]
use crate::{BezierCurve, Point};

// 由N个点组成的polyline转换成bezier curve
// close: 是否是闭合曲线
// offset_a: 平滑度a
// offset_b: 平滑度b
pub fn polyline_to_bezier_curve(
    polyline: &[Point],
    close: bool,
    offset_a: f64,
    offset_b: f64,
) -> BezierCurve {
    debug_assert!(polyline.len() > 2);
    let start_point = polyline[0];
    let bezier_curve_line_num = polyline.len() - 1;
    let mut bezier_curve_point = (0..bezier_curve_line_num)
        .enumerate()
        .map(|(i, _)| {
            let ret = get_bezier_curve_line_control_points(polyline, i, close, offset_a, offset_b);
            [ret[0], ret[1], polyline[i + 1]]
        })
        .collect::<Vec<_>>();
    if close {
        close_bezier_curve(&mut bezier_curve_point, start_point);
    }
    BezierCurve {
        start_point,
        segments: bezier_curve_point,
    }
}

// 获取贝塞尔的控制点
// index: 控制点在polyline中的索引位置
fn get_bezier_curve_line_control_points(
    polyline: &[Point],
    index: usize,
    close: bool,
    offset_a: f64,
    offset_b: f64,
) -> [Point; 2] {
    let point_num = polyline.len();
    debug_assert!(point_num >= 3 && index < point_num);
    let mut before_point_index = index as isize - 1;
    if before_point_index < 0 {
        before_point_index = if close {
            point_num as isize + before_point_index
        } else {
            0
        };
    }

    let mut after_point_index = index + 2;
    if after_point_index >= point_num {
        after_point_index = if close {
            after_point_index - point_num
        } else {
            point_num - 1
        };
    }

    let mut after_next_point_index = index + 2;
    if after_next_point_index >= point_num {
        after_next_point_index = if close {
            after_next_point_index - point_num
        } else {
            point_num - 1
        };
    }

    let point_before = polyline[before_point_index as usize];
    let point_middle = polyline[index];
    let point_after = polyline[after_point_index];
    let point_after_next = polyline[after_next_point_index];
    [
        [
            point_middle[0] + offset_a * (point_after[0] - point_before[0]),
            point_middle[1] + offset_a * (point_after[1] - point_before[1]),
        ],
        [
            point_after[0] - offset_b * (point_after_next[0] - point_middle[0]),
            point_after[1] - offset_b * (point_after_next[1] - point_middle[1]),
        ],
    ]
}

// 获取最后闭合的数据
fn close_bezier_curve(bezier_curve: &mut Vec<[Point; 3]>, start_point: Point) {
    let first_sub_curve = bezier_curve[0];
    let last_sub_curve = bezier_curve[bezier_curve.len() - 1];

    bezier_curve.push([
        get_symmetry_point(last_sub_curve[1], last_sub_curve[2]),
        get_symmetry_point(first_sub_curve[0], start_point),
        start_point,
    ]);
}

// 获取对称点
fn get_symmetry_point(point: Point, center_point: Point) -> Point {
    let (px, py) = (point[0], point[1]);
    let (cx, cy) = (center_point[0], center_point[1]);

    let minus_x = cx - px;
    let minus_y = cy - py;
    [cx + minus_x, cy + minus_y]
}
